#
# Kurella/Rapp: The role of issue salience and competitive advantages in spatial models of political competition
#
# Replication Material: Figure 7, Table 4
#
#
######################################

################################################################################
#
# Load required Packages 
#

library(haven)
library(ggplot2)
library(ggpubr)
library(ggeffects)
library(stargazer)


################################################################################
#
# Read data
#

ches <- read_dta("1999-2019_CHES_dataset_means(v2).dta")
ches <- as.data.frame(ches)


################################################################################
#
# Data Preparation
#

# Only Western European countries
ches <- ches[ches$eastwest==1,]

# Country election year identifier
ches$id <- paste(ches$country, ches$electionyear, sep="_")

# Time variable
ches$t <- as.numeric(as.factor(ches$year))

# Lag vote share as weights
library(plm)
pdta <- pdata.frame(ches, index= c("party_id", "t"))
ches$lag_vote <- lag(pdta$vote)

ches$weight_immi <- NA
ches$weight_env <- NA

for(i in unique(ches$id)){
  ches$weight_immi[ ches$id==i  ] <- 
    ches$lag_vote[ches$id==i  ] / sum(ches$lag_vote[ches$id==i ], na.rm=TRUE)
  ches$weight_env[ ches$id==i   ] <- 
    ches$lag_vote[ches$id==i   ] / sum(ches$lag_vote[ches$id==i ], na.rm=TRUE)
}


for(i in unique(ches$id)){
  tmp <- ches[ches$id==i,]
  if(dim(tmp)[1]>1){ # no of obs. > 1
    # problem: for some parties no weight because not yet founded or included in ches in the prior election
    ches$sal_immi[ches$id==i] <- sum(tmp$immigrate_salience[!is.na(tmp$vote)& !is.na(tmp$weight_immi)]*tmp$weight_immi[!is.na(tmp$vote) & !is.na(tmp$weight_immi)]/100)
    ches$sal_envir[ches$id==i] <- sum(tmp$enviro_salience[!is.na(tmp$vote)& !is.na(tmp$weight_env)]*tmp$weight_env[!is.na(tmp$vote) & !is.na(tmp$weight_env)]/100)
    
    if(any(tmp$family==1)){
      if(any(!is.na(tmp$immigrate_policy[tmp$family==1]))){
        right <- tmp$immigrate_policy[tmp$family==1]
        ches$dist_competitor[ches$id==i & ches$family==1] <- min(abs(tmp$immigrate_policy[tmp$family!=1]-right))}}
    if(any(tmp$family==7)){
      if(any(!is.na(tmp$environment[tmp$family==7]))){
        green <- tmp$immigrate_policy[tmp$family==1]
        ches$dist_competitor[ches$id==i & ches$family==7] <- min(abs(tmp$environment[tmp$family!=7]-green))
      }}}}

# only radical right and green parties
ches <- ches[ches$family==1 | ches$family==7,]

# one common salience variable
ches$sal <- ches$sal_immi
ches$sal[ches$family==7] <- ches$sal_envir[ches$family==7]

# one common position variable
ches$niche_position <- ches$immigrate_policy
ches$niche_position[ches$family==7] <- -1*ches$environment[ches$family==7]+10

# position on economic position
ches$mod_econ_position <- abs(ches$lrecon-5)

# vote share variable
ches$vote <- ches$vote/100

# only cases without na
ches <- ches[!is.na(ches$vote),]
ches <- ches[ches$dist_competitor!=Inf,]
ches <- ches[!is.na(ches$sal),]

# only include cases with minimum 2 observations
n <- table(ches$party_id)
n <- n[n>1]
keep <- as.numeric(names(n))

ches$keep <- NA
for(i in 1:dim(ches)[1]){
  ches$keep[i] <- ifelse(any(keep==ches$party_id[i]), 1, 0)
}
ches <- ches[ches$keep==1,]

ches$party_id <- as.factor(ches$party_id)


################################################################################
#
# Model Estimation
#

# Model 1: Base
m_1 <- lm(vote ~    sal + party_id ,
           data=ches)

summary(m_1)

# Model 2: Extremeness
m_2 <- lm(vote ~    sal +
             sal*niche_position + party_id ,
           data=ches)

summary(m_2)


# Model 3: Moderateness
m_3 <- lm(vote ~    sal +
             sal*mod_econ_position + party_id,
           data=ches)

summary(m_3)


# Model 4: Uniqueness
m_4 <- lm(vote ~    sal +
             sal *dist_competitor + party_id  ,
           data=ches)

summary(m_4)


# Model 5: Full model
m_5 <- lm(vote ~    sal +
            sal *dist_competitor  +
            sal*niche_position + sal*mod_econ_position + party_id,
          data=ches)

summary(m_5)




################################################################################
# Figure 7

# a) Extremeness
p1 <- ggpredict(m_5,
                terms = c("sal","niche_position[8,10]"),
                ci.lvl = 0.95)

p11 <- as.data.frame(p1) 
p11$group <- as.factor(p11$group) 

plot_extreme  <- ggplot(data=p11, aes(x=x, y=predicted, color=group))+ 
  geom_line(aes(color=group), lwd=1.2)+
  
  geom_ribbon(aes(ymin=p1$conf.low, ymax=p1$conf.high,color=group), lty=2, alpha=0.01)+ 
  scale_colour_manual(values=c("darkblue", "lightblue" )) + 
  labs( 
    title="", 
    x= "Salience", 
    y= "Core issue party vote share", 
    color= "Extremeness on core issue", 
    fill="Extremeness on core issue", 
    linetype="Extremeness on core issue")+ 
  theme_classic()+ 
  theme(
    element_line(colour="black"),
    text=element_text(size=17), legend.position="top") 

# b) Economic Moderation
p2 <- ggpredict(m_5,
                terms = c("sal", "mod_econ_position[0,3]"),
                ci.lvl = 0.95)

p12 <- as.data.frame(p2) 
p12$group <- as.factor(p12$group) 

plot_econ  <- ggplot(data=p12, aes(x=x, y=predicted, color=group))+
  geom_line(aes(color=group), lwd=1.2)+ 
  
  geom_ribbon(aes(ymin=p2$conf.low, ymax=p2$conf.high,color=group), lty=2, alpha=0.01)+ 
  scale_colour_manual(values=c("darkblue", "lightblue" )) + 
  labs( 
    title="",
    x= "Salience", 
    y= "Core issue party vote share", 
    color= "Extremeness on econ issue", 
    fill="Extremeness on econ issue", 
    linetype="Extremeness on econ issue")+ 
  theme_classic()+ 
  theme(
    element_line(colour="black"),
    text=element_text(size=17), legend.position="top") 

# c) Uniqueness
p3 <- ggpredict(m_5,
                terms = c("sal","dist_competitor[0.6,4.4]"),
                ci.lvl = 0.95)

p13 <- as.data.frame(p3) 
p13$group <- as.factor(p13$group) 

plot_dist  <- ggplot(data=p13, aes(x=x, y=predicted, color=group))+ 
  geom_line(aes(color=group), lwd=1.2)+ 
  
  geom_ribbon(aes(ymin=p3$conf.low, ymax=p3$conf.high,color=group), lty=2, alpha=0.01)+ 
  scale_colour_manual(values=c("lightblue", "darkblue")) + 
  labs( 
    title="", 
    x= "Salience", 
    y= "Core issue party vote share", 
    color= "Distance to competitor", 
    fill="Distance to competitor", 
    linetype="Distance to competitor")+ 
  theme_classic()+ 
  theme(
    element_line(colour="black"),
    text=element_text(size=17), legend.position="top") 

# arrange plots
ggarrange(plot_extreme, plot_econ, plot_dist ,  
          font.label = list(size = 20, face = "bold"),
          labels = c("a) Extremeness", "b) Moderation", "c) Uniqueness"),
          ncol = 3, nrow = 1)


################################################################################
# Table 4

stargazer(m_1,m_2,m_3, m_4, m_5,
          header=F, 
          type= "latex", dep.var.labels = "Core issue party vote share",
          title= "Core issue party vote", notes = "standard errors in parentheses")

